import React, { useEffect, useState } from "react";
import '../../appcommon.css';
import styles from './todayearndetail.module.css';
import { useDispatch } from "react-redux";
import { DotLoading, Mask, Toast } from "antd-mobile";
import { saveEvent } from "@/store/actions/saveEvent";
import { useHistory } from "react-router-dom/cjs/react-router-dom.min";


import NavigateBar from "@/components/NavigateBar";
import { getCoinInfo, isUnvalidUserInfo } from "@/utils/storage";
import { getTransferInEarnList } from "@/store/actions/user";
import EmptyView from "@/components/EmptyView/emptyView";
import { LangThai } from '@/utils/langThai';


/**
 * 登录页
 */
export default function TodayEarnDetail() {

    //loading状态
    const [maskVisible, setMaskVisible] = useState(false)

    //显示add home弹框
    const [addHomeMaskVisible, setAddHomeMaskVisible] = useState(false);
    const [addHomeMaskIsHomeMode, setAddHomeMaskIsHomeMode] = useState(true);

    // 当前 列表
    const [curList, setCurList] = useState([]);

    // 当前 币
    const [curCoin, setCurCoin] = useState({})

    // 文案翻译
    const [curLang, setCurLang] = useState(LangThai);

    // 获取 Redux 分发器
    const dispatch = useDispatch()

    //router
    const history = useHistory()

    //清空登录信息
    useEffect(() => {
        //关闭loading
        setMaskVisible(false);

        // 判断是否需要重登录
        if (isUnvalidUserInfo()) {
            history.replace('/login')
            return
        }

        // 获取当前币
        const coinInfo = getCoinInfo();
        const coninType = coinInfo.coinType || "";
        setCurCoin(coinInfo);

        // 获取转入收益列表
        doGetTransferInEarnList(coninType);

        //打点
        doHitEvent("TODAY_EARN_DETAIL_START")

        return () => {
            //打点
            doHitEvent("TODAY_EARN_DETAIL_END")
        }
    }, [])


    // 获取转入收益列表 返回
    const onDoGetTransferInEarnListCallback = (success, data, isNeedToReLogin) => {
        //关闭loading
        setMaskVisible(false)
        console.log('获取转入收益列表 返回: ' + success);

        if (success && data) {

            // 测试
            if (false) {
                for (let i = 0; i < 10; i++) {
                    let item = {
                        createTime: "timexxx",
                        income: "incomexxx",
                        amount: "amountxxx",
                        rate: 'ratexxx'
                    };
                    data.push(item);
                }
            }

            // 处理流程业务
            if (data.length > 0) {
                // 更新列表
                setCurList(data);
            }
        } else {
            if (isNeedToReLogin) {
                history.replace('/login')
            }
        }
    };


    //打点接口
    const doHitEvent = async (type, remark = "") => {
        console.log("doHitEvent: " + type + " , " + remark);

        try {
            //获取Action
            const action = saveEvent(type, remark)

            //触发接口
            await dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }

    // 获取转入收益列表
    const doGetTransferInEarnList = async (coinType) => {
        console.log("doGetTransferInEarnList: " + coinType);

        if (!coinType || coinType.length <= 0) {
            Toast.show(curLang.earnTheCoinTypeCanNotBeNull);
            return;
        }

        try {
            //显示loading
            setMaskVisible(true)

            //获取Action
            const action = getTransferInEarnList(coinType, onDoGetTransferInEarnListCallback)

            //触发接口
            await dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }


    return (
        <div className='pageRoot pageBackgroundColorGray'>
            <NavigateBar theme="white" showLogout={false} onAddFavorite={() => { setAddHomeMaskIsHomeMode(false); setAddHomeMaskVisible(true) }} onAddHome={() => { setAddHomeMaskIsHomeMode(true); setAddHomeMaskVisible(true) }}>{curLang.todayEarnDetailEarnDetail}</NavigateBar>

            {/* 使用添加 home screen bar */}
            {/* <AddFavorite onAddHome={() => { setAddHomeMaskIsHomeMode(true); setAddHomeMaskVisible(true) }} /> */}

            <div className="pageContentColumnFull">
                {/* 菜单 列表 */}
                <div className="pageContentColumnFull">
                    <div className={styles.todayEarnDetailList}>
                        {curList && curList.map((value, index) => {
                            return (
                                <div className={styles.todayEarnDetailListItem} key={index}>
                                    <div className={styles.todayEarnDetailListItemRow}>
                                        <div className={`${styles.todayEarnDetailListItemTextRow} pageMarginTop8px`}>
                                            <div className={styles.todayEarnDetailListItemTitle}></div>
                                            <div className={styles.todayEarnDetailListItemDetail}>{value.createTime}</div>
                                        </div>

                                        <div className={styles.todayEarnDetailListItemTextRow}>
                                            <div className={styles.todayEarnDetailListItemTitle}>{curLang.todayEarnDetailEarnAmount}</div>
                                            <div className={styles.todayEarnDetailListItemDetailBold}>{value.income} USDT</div>
                                        </div>

                                        <div className={styles.todayEarnDetailListItemTextRow}>
                                            <div className={styles.todayEarnDetailListItemTitle}>{curLang.todayEarnDetailTransferInAmount}</div>
                                            <div className={styles.todayEarnDetailListItemDetail}>{value.amount} USDT</div>
                                        </div>

                                        <div className={styles.todayEarnDetailListItemTextRow}>
                                            <div className={styles.todayEarnDetailListItemTitle}>{curLang.todayEarnDetailEarnRatio}</div>
                                            <div className={styles.todayEarnDetailListItemDetail}>{value.rate}</div>
                                        </div>

                                        <div className={styles.todayEarnDetailListItemTextRow}>
                                            <div className={styles.todayEarnDetailListItemTitle}>{curLang.todayEarnDetailStatus}</div>
                                            <div className={styles.todayEarnDetailListItemStatus}>{curLang.todayEarnDetailSuccess}</div>
                                        </div>
                                    </div>
                                </div>
                            )
                        })}
                    </div>

                    {(!curList || (curList.length === 0)) && <EmptyView></EmptyView>}

                </div>
            </div>

            {/* 底部 margin */}
            <div className="pageBottomEdge"></div>

            {/* load mask */}
            <Mask visible={maskVisible} opacity={0.1}
                onMaskClick={() => { }}>
                <div className="pageLoadingOverlayContent">
                    <span style={{ fontSize: 24, color: '#3498DB' }} >
                        <DotLoading color="currentColor" />
                    </span>
                </div>
            </Mask>
        </div >
    )
}